# -*- coding: utf-8 -*-
class Box_init():
    def __init__(self, m_client, redis_client):
        ...
        self.m_client = m_client

        self.config_D = m_client.config_D
        self.config_J = m_client.config_J
        self.config_A = m_client.config_A

        self.func_client_D = self.m_client.func_client_D
        self.func_client_J = self.m_client.func_client_J
        self.func_client_A = self.m_client.func_client_A

        self.redis_client = redis_client

    def box_delete(self, func_client, repo_type):
        result = func_client.get_repo_all()
        if result.get("status") != 200:
            raise Exception(result.get("message"))
        exist_repo_list = result.get("message")
        if exist_repo_list is not None:
            print("清除%s盒子库中..." % repo_type)
            for repo in exist_repo_list:
                func_client.delete_repo(repo)

    def creat_repo(self, func_client, repo_type, config):
        print("创建%s盒子库中..." % repo_type)
        func_client.create_repo(config.get('ID1'), '%s_1' % repo_type)
        func_client.create_repo(config.get('ID2'), '%s_2' % repo_type)
        func_client.create_repo(config.get('ID3'), '%s_3' % repo_type)

    # 盒子初始化 分开初始化
    def box_init(self):
        # 删除 D J A
        self.box_delete(self.func_client_D, 'D')
        self.box_delete(self.func_client_J, 'J')
        self.box_delete(self.func_client_A, 'A')

        # 创建 repo
        self.creat_repo(self.func_client_D, 'D', self.config_D)
        self.creat_repo(self.func_client_J, 'J', self.config_J)
        self.creat_repo(self.func_client_A, 'A', self.config_A)

        # 初始化mongo
        r1 = self.init_mongo()
        # 初始化 redis
        # r2 = self.init_redis()
        if r1:  # and r2:
            return True
        else:
            return False

    def init_mongo(self):
        try:
            print("初始化mongo中...")
            client = self.m_client
            # client.drop_all()
            client.init_mongo()
        except Exception as e:
            print("初始化mongo失败", str(e))
            raise Exception(str(e))
        else:
            print("初始化mongo完毕")
            return True

    def init_redis(self):
        try:
            print("清除redis数据")
            client = self.redis_client
            result = client.flush_all()
        except Exception as e:
            print("初始化redis失败", str(e))
            raise Exception(str(e))
        else:
            print("初始化redis完毕")
            return result
